<?php

declare(strict_types=1);

namespace app\backend\controller\crm;

use app\common\controller\Basic;
use app\common\dao\crm\ReceivablesDao;
use app\common\dao\crm\ContractDao;
use app\common\dao\sys\DictDao;
use app\common\dao\crm\CustomerDao;
use app\common\dao\crm\RecordDao;
use app\common\dao\crm\ClueDao;

class Console extends Basic
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $data = [];
        $data['customer_number'] = (new CustomerDao())->getAddNumberByWeek($this->user['id']);
        $data['record_number'] = (new RecordDao())->getAddNumberByWeek($this->user['id']);
        $data['clue_number'] = (new ClueDao())->getAddNumberByWeek($this->user['id']);
        $data['open_number'] = (new CustomerDao())->getAddNumberByWeekOpen($this->user['id']);
        $data['contract_number'] = (new ContractDao())->getAddNumberByWeek($this->user['id']);
        $data['receivables_number'] = (new ReceivablesDao())->getAddNumberByWeek($this->user['id']);
        return _ok("获取成功", $data);
    }

    public function getCustomer()
    {
        $source = [];
        $data = [];
        $month = date("Y-m");
        $xAxis = [];
        $xAxis[0] = "product";
        for ($i = 11; $i >= 0; $i--) {
            $xAxis[12 - $i] = date("Y-m", strtotime("-{$i} month", time()));
        }

        $_list = (new CustomerDao())->getSourceByMonth($this->user['id']);
        $source[] = $xAxis;
        $list = (new DictDao())->getDict('108');
        $series = [];
        $temp = [];
        $temp['type'] = 'pie';
        $temp['id'] = 'pie';
        $temp['radius'] = '30%';
        $temp['center'] = ['50%', '25%'];
        $temp['emphasis'] = ['focus' => 'self'];
        $last_month = date("Y-m");
        $temp['label'] = ['formatter' => '{b}: {@' . $month . '} ({d}%)'];
        $temp['encode'] = ['itemName' => 'product', 'value' => $month, 'tooltip' => $month];
        $series[] = $temp;
        foreach ($list as $k => $v) {
            foreach ($v as $k2 => $v2) {
                $temp = [];
                $type = $v2['value'];
                foreach ($xAxis as $k3 => $v3) {
                    if ($k3 == 0) {
                        $temp[0] = $v2['name'];
                    } else {
                        $temp[$k3] = isset($_list[$type][$v3]) ? $_list[$type][$v3] : 0;
                    }
                }
                $source[] = $temp;
                $temp = [];
                $temp['type'] = 'line';
                $temp['smooth'] = true;
                $temp['seriesLayoutBy'] = 'row';
                $temp['emphasis'] = ['focus' => 'series'];
                $series[] = $temp;
            }
        }
        $data = [];
        $data['source'] = $source;
        $data['series'] = $series;
        return _ok("获取成功", $data);
    }


    public function getContract()
    {
        $data = [];
        $month = date("Y-m");
        $xAxis = [];
        for ($i = 0; $i <= 11; $i++) {
            $xAxis[12 - $i] = date("Y-m", strtotime("-{$i} month", time()));
        }
        asort($xAxis);
        $data['xAxis'] = $xAxis;

        $user_id = $this->user['id'];;

        $_yAxis_left = (new ContractDao())->getMoneyByGroupMonth($user_id);
        $yAxis_left = [];
        $max_left = 0;
        foreach ($xAxis as $k => $v) {
            if (isset($_yAxis_left[$v])) {
                $yAxis_left[$k] = $_yAxis_left[$v];
                if ($max_left < $_yAxis_left[$v]) {
                    $max_left = $_yAxis_left[$v];
                }
            } else {
                $yAxis_left[$k] = "0.00";
            }
        }
        $data['yAxis_left'] = $yAxis_left;
        $data['max_left'] = $max_left;

        $_yAxis_right = (new ContractDao())->getTotalByGroupMonth($user_id);
        $yAxis_right = [];
        $max_right = 0;
        foreach ($xAxis as $k => $v) {
            if (isset($_yAxis_right[$v])) {
                $yAxis_right[$k] = $_yAxis_right[$v];
                if ($max_right < $_yAxis_right[$v]) {
                    $max_right = $_yAxis_right[$v];
                }
            } else {
                $yAxis_right[$k] = "0";
            }
        }
        $data['yAxis_right'] = $yAxis_right;
        $data['max_right'] = $max_right;

        return _ok("获取成功", $data);
    }

    public function getReceivables()
    {
        $data = [];
        $month = date("Y-m");
        $xAxis = [];
        for ($i = 0; $i <= 11; $i++) {
            $xAxis[12 - $i] = date("Y-m", strtotime("-{$i} month", time()));
        }
        asort($xAxis);
        $data['xAxis'] = $xAxis;

        $user_id = $this->user['id'];;

        $_yAxis_left = (new ReceivablesDao())->getMoneyByGroupMonth($user_id);
        $yAxis_left = [];
        $max_left = 0;
        foreach ($xAxis as $k => $v) {
            if (isset($_yAxis_left[$v])) {
                $yAxis_left[$k] = $_yAxis_left[$v];
                if ($max_left < $_yAxis_left[$v]) {
                    $max_left = $_yAxis_left[$v];
                }
            } else {
                $yAxis_left[$k] = "0.00";
            }
        }
        $data['yAxis_left'] = $yAxis_left;
        $data['max_left'] = $max_left;

        $_yAxis_right = (new ReceivablesDao())->getTotalByGroupMonth($user_id);
        $yAxis_right = [];
        $max_right = 0;
        foreach ($xAxis as $k => $v) {
            if (isset($_yAxis_right[$v])) {
                $yAxis_right[$k] = $_yAxis_right[$v];
                if ($max_right < $_yAxis_right[$v]) {
                    $max_right = $_yAxis_right[$v];
                }
            } else {
                $yAxis_right[$k] = "0";
            }
        }
        $data['yAxis_right'] = $yAxis_right;
        $data['max_right'] = $max_right;

        return _ok("获取成功", $data);

    }
}